package com.common.code.msgpack;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import org.msgpack.MessagePack;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MsgPackEncode extends MessageToByteEncoder<Object> {

    private final static Logger logger = LoggerFactory.getLogger(MsgPackEncode.class);
    @Override
    protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) throws Exception {
        try {
            logger.info("encode start");
            MessagePack msgPack = new MessagePack();
            byte[] raw = msgPack.write(msg);
            out.writeBytes(raw);
            logger.info("encode end");
        }catch(Exception e){
            logger.error("encode exception {}",e);
        }
    }
}
